<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-button type="success"> 添加角色</el-button>
    <!-- 卡片视图 -->
    <el-table :data="rolesList"
              style="width: 100%"
              border>
      <el-table-column label="#"
                       type="expand">
        <template slot-scope="scope">
          <!-- 格式化JSON数据 -->
          <!-- <pre>
          {{scope.row}}
            </pre> -->
          <el-row :class="['bdbottom',i1 === 0?'bdtop':'','vcenter']"
                  v-for="(item1,i1) in scope.row.children"
                  :key="item1.id">
            <!-- 渲染一级权限 -->
            <el-col :span="5">
              <el-tag closable
                      @click="removeRight(scope.row,item1.id)">{{item1.authName}}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 渲染二级和三级权限 -->
            <el-col :span="19">
              <el-row :class="[i2 === 0?'':'bdtop','vcenter']"
                      v-for="(item2,i2) in item1.children"
                      :key="item2.id">
                <el-col :span="6">
                  <el-tag type="success"
                          closable
                          @click="removeRight(scope.row,item2.id)">{{item2.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="18">
                  <el-tag closable
                          type="warning"
                          v-for="(item3) in item2.children"
                          @click="removeRight(scope.row,item3.id)"
                          :key="item3.id">{{item3.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="#"
                       type="index">
      </el-table-column>

      <el-table-column prop="roleName"
                       label="角色名称">
      </el-table-column>
      <el-table-column prop="roleDesc"
                       label="角色描述">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">

          <el-button type="primary"
                     icon="el-icon-edit"></el-button>
          <el-button type="danger"
                     icon="el-icon-delete"></el-button>
          <el-button type="success"
                     icon="el-icon-setting"
                     @click="getRightsTree(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分配权限的对话框 -->
    <el-dialog title="分配权限"
               :visible.sync="dialogFormVisible"
               @close="addDialogClosed">
      <el-tree :data="rightsTess"
               show-checkbox
               node-key="id"
               ref="treeRef"
               :default-checked-keys="defKeys"
               :props="defaultProps"
               @node-click="handleNodeClick"></el-tree>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary"
                   @click="submitRights()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rolesList: [],
      // 控制权限分配提示框的出现和隐藏
      dialogFormVisible: false,
      //   树形控件树形
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      rightsTess: [],
      defKeys: [],
      roleId: ''
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      this.rolesList = res.data
      console.log(this.rolesList)
    },
    async removeRight (role, rightId) {
      console.log(role, rightId)

      const isConfirm = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      console.log(isConfirm)
      if (isConfirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${rightId}`
      )
      if (res.meta.status !== 200) {
        return this.$message.error('删除权限失败')
      }
      role.children = res.data
    },
    async getRightsTree (role) {
      this.roleId = role.id
      this.dialogFormVisible = true
      const { data: res } = await this.$http.get('rights/tree')
      this.rightsTess = res.data
      this.getLeafKeys(role, this.defKeys)
    },
    addDialogClosed () {
      this.defKeys = []
    },
    async submitRights () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedNodes()
      ]
      const idsStr = keys.join(',')
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idsStr }
      )
      this.getRolesList()
      this.dialogFormVisible = false
    },
    handleNodeClick () {},
    getLeafKeys (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => this.getLeafKeys(item, arr))
    }
  }
}
</script>

<style>
.el-table {
  margin: 15px;
}
.el-tag {
  margin: 10px;
}

.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
